package com.hymjweb.demo.hymj.customer_manage.freight_agreement.air.mapper;

import com.hymjweb.demo.framework.util.DataTypeUtil;
import com.hymjweb.demo.hymj.customer_manage.freight_agreement.air.bean.BCustomerAirFreightAgreementPojo;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.jdbc.SQL;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;

@Mapper
@Component
public interface BCustomerAirFreightAgreementMapper {


    class BCustomerAirFreightAgreementMapperProvider{
        public String findAll(Map map){

            String sql= new SQL() {{
                SELECT("t.*");
                FROM("b_customer_air_freight_agreement t ");
                WHERE("1=1");
            }}.toString();

            if(map!=null){
                /**
                 * let keyDeliveryCustomerId = mini.get("keyDeliveryCustomerId");
                 *             let keyCityId = mini.get("keyCityId");
                 *             let keyAirportId = mini.get("keyAirportId");
                 *             let keyAirlinesId = mini.get("keyAirlinesId");
                 *             let keyFlightNo = mini.get("keyFlightNo");
                 */

                String keyDeliveryCustomerId = DataTypeUtil.toStringObject(map.get("keyDeliveryCustomerId"));
                String keyCityId = DataTypeUtil.toStringObject(map.get("keyCityId"));
                String keyAirportId = DataTypeUtil.toStringObject(map.get("keyAirportId"));
                String keyAirlinesId = DataTypeUtil.toStringObject(map.get("keyAirlinesId"));
                String keyFlightNo = DataTypeUtil.toStringObject(map.get("keyFlightNo"));

                if(keyDeliveryCustomerId != null){
                    sql += " and t.customer_id = " + DataTypeUtil.quoteStr(keyDeliveryCustomerId);
                }

                if(keyCityId != null){
                    sql += " and t.city_id = " + DataTypeUtil.quoteStr(keyCityId);
                }

                if(keyAirportId != null){
                    sql += " and t.airport_id = " + DataTypeUtil.quoteStr(keyAirportId);
                }

                if(keyAirlinesId != null){
                    sql += " and t.airlines_id = " + DataTypeUtil.quoteStr(keyAirlinesId);
                }

                if(keyFlightNo != null){
                    sql += " and t.flight_no = " + DataTypeUtil.quoteAllLikeStr(keyFlightNo);
                }
            }

            return sql;
        }

        public String getAirFreightAgreementPojoByIds(String ids){
            String sql= new SQL() {{
                SELECT("t.*");
                FROM("b_customer_air_freight_agreement t ");
                WHERE("1=1 and t.id in (" + ids + ")");
            }}.toString();
            return sql;
        }

        // BCustomerAirFreightAgreementPojo queryByUK(String customerId, String airportId, String cityId, String airlinesId, String flightNo);
        public String getAirFreightAgreementPojo(BCustomerAirFreightAgreementPojo agreementPojo){
            String sql= new SQL() {{
                SELECT("t.*");
                FROM("b_customer_air_freight_agreement t ");
                WHERE("1=1 and t.customer_id = #{customerId} and t.airport_id = #{airportId} and t.city_id = #{cityId} and t.airlines_id = #{airlinesId} and t.flight_no = #{flightNo}");
                // customerId airportId  cityId  airlinesId flightNo (UK)
            }}.toString();
            return sql;
        }


        public String queryPojoByUk(BCustomerAirFreightAgreementPojo pojo){
            // customerId airportId  cityId  airlinesId flightNo (UK)
            String sql= new SQL() {{
                SELECT("t.*");
                FROM("b_customer_air_freight_agreement t ");
                WHERE("1=1 and t.customer_id = #{customerId} and t.airport_id = #{airportId} and t.city_id = #{cityId} and t.airlines_id = #{airlinesId} and t.flight_no = #{flightNo}");
            }}.toString();
            return sql;
        }

        public String insert(BCustomerAirFreightAgreementPojo airAgreementPojo){
            String sql = new SQL() {{
                INSERT_INTO("b_customer_air_freight_agreement");
                VALUES("id","#{id}");
                VALUES("customer_id","#{customerId}");
                VALUES("airport_id","#{airportId}");
                VALUES("city_id","#{cityId}");
                VALUES("airlines_id","#{airlinesId}");
                VALUES("flight_no","#{flightNo}");
                VALUES("file_id","#{fileId}");
                VALUES("comment","#{comment}");
                VALUES("create_time", "#{createTime}");
                VALUES("create_user_id", "#{createUserId}");
                VALUES("update_time", "#{updateTime}");
                VALUES("update_user_id", "#{updateUserId}");
            }}.toString();
            return sql;
        }

        public String update(BCustomerAirFreightAgreementPojo airAgreementPojo){
            String sql= new SQL() {{
                UPDATE("b_customer_air_freight_agreement t");
                SET("t.customer_id = #{customerId}");
                SET("t.airport_id = #{airportId}");
                SET("t.city_id = #{cityId}");
                SET("t.airlines_id = #{airlinesId}");
                SET("t.flight_no = #{flightNo}");
                SET("t.comment = #{comment}");
                SET("t.update_time = #{updateTime}");
                SET("t.update_user_id = #{updateUserId}");
                WHERE("t.id = #{id}");
            }}.toString();
            return sql;
        }

        public String deleteByIds(String agreementFreightIds){
            String sql = new SQL() {{
                DELETE_FROM("b_customer_air_freight_agreement t");
                WHERE("t.id in (" + agreementFreightIds + ")");
            }}.toString();
            return sql;
        }

    }


    @Results(id = "bCustomerAirFreightAgreementMapId",value = {
            @Result(column = "id", property = "id"),
            @Result(column = "customer_id", property = "customerId"),
            @Result(column = "airport_id", property = "airportId"),
            @Result(column = "city_id", property = "cityId"),
            @Result(column = "airlines_id", property = "airlinesId"),
            @Result(column = "flight_no", property = "flightNo"),
            @Result(column = "file_id", property = "fileId"),
            @Result(column = "comment", property = "comment"),
            @Result(column = "create_time", property = "createTime"),
            @Result(column = "create_user_id", property = "createUserId"),
            @Result(column = "update_time", property = "updateTime"),
            @Result(column = "update_user_id", property = "updateUserId")
    })
    @SelectProvider(type = BCustomerAirFreightAgreementMapperProvider.class, method = "findAll")
    List<BCustomerAirFreightAgreementPojo> findAll(Map param);

    @ResultMap("bCustomerAirFreightAgreementMapId")
    @SelectProvider(type = BCustomerAirFreightAgreementMapperProvider.class, method = "getAirFreightAgreementPojoByIds")
    List<BCustomerAirFreightAgreementPojo> getAirFreightAgreementPojoByIds(String ids);


    @ResultMap("bCustomerAirFreightAgreementMapId")
    @SelectProvider(type = BCustomerAirFreightAgreementMapperProvider.class, method = "getAirFreightAgreementPojo")
    BCustomerAirFreightAgreementPojo getAirFreightAgreementPojo(BCustomerAirFreightAgreementPojo agreementPojo);

    @ResultMap("bCustomerAirFreightAgreementMapId")
    @SelectProvider(type = BCustomerAirFreightAgreementMapperProvider.class, method = "queryPojoByUk")
    BCustomerAirFreightAgreementPojo queryPojoByUk(BCustomerAirFreightAgreementPojo pojo);

    /**
     * id, customer_id, airport_id, city_id, airlines_id, flight_no, file_id, comment, create_time, create_user_id, update_time, update_user_id
     * @param airFreightAgreementPojos
     * @return
     */

    @Insert(
            "<script>" +
                    "insert into b_customer_air_freight_agreement(id, customer_id, airport_id, city_id, airlines_id, flight_no, file_id, comment, create_time, create_user_id, update_time, update_user_id) values " +
                    "<foreach collection='agreementPojos' item='pojo' separator=','>" +
                    "(#{pojo.id}, #{pojo.customerId}, #{pojo.airportId}, #{pojo.cityId}, #{pojo.airlinesId}, #{pojo.flightNo}, #{pojo.fileId}, #{pojo.comment}, #{pojo.createTime}, #{pojo.createUserId}, #{pojo.updateTime}, #{pojo.updateUserId})" +
                    "</foreach>" +
            "</script>"
    )
    @Options(keyProperty = "id")
    int batchInsert(@Param("agreementPojos") List<BCustomerAirFreightAgreementPojo> airFreightAgreementPojos);


    @InsertProvider(type = BCustomerAirFreightAgreementMapperProvider.class, method = "insert")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    int insert(BCustomerAirFreightAgreementPojo airAgreementPojo);


    @UpdateProvider(type = BCustomerAirFreightAgreementMapperProvider.class, method = "update")
    int update(BCustomerAirFreightAgreementPojo airAgreementPojo);


    @DeleteProvider(type = BCustomerAirFreightAgreementMapperProvider.class, method = "deleteByIds")
    int deleteByIds(String agreementFreightIds);


}
